/**
*	企业路由处理函数
*	@A1uthor Apricity
**/
"use strict";

const { getToken } = require('../utils/jwtFun');

const urllib = require("urllib");

const { setCrypto, getCrypto } = require('../utils/crypto.js');

const cloudbase = require("@cloudbase/node-sdk");

const app = cloudbase.init({
    env: "work-xiangxiao-1gtlbaah05be1c75"
});

const db = app.database();

/**
 *  企业发送验证码
 */
const getMobileNote = async (req,res) => {
    try {
        
    } catch (e) {
        return {status: 400,message: 'FAIL',err: e}
    }
} 


/*
	企业注册	
*/
const businessRegister = async (req,res) => {
    try {
	let time = new Date();
    let password = setCrypto(req.password);
	let obj = {
		nickName: req.nickName,
		accounts: req.accounts,
		password: password,
		isDel: 0,
		time: time,
		info: req.info
	};
	const res = await db.collection("Business").add(obj);
	return {...res,status: 200,message: 'success'};
    } catch (e) {
        return {status: 400,message: 'FAIL',err: e}
    }
}

/**
 *  企业登陆
 */
const businessLogin = async (req,res) => {
    try {
        const {accounts,password} = req;
        const res = await db.collection("Business").where({accounts: accounts}).get();
        const oldPassword = res.data[0].password;
        const result = getCrypto(password,oldPassword);
        if (!result)  return {status: 400,message: '账号或密码不正确'};
        const token = getToken(accounts);
        const obj = {
            info: res.data[0].info,
            nickName: res.data[0].nickName
        }
        return {status: 200,message: '登陆成功',data: obj,token: token};
    } catch (e) {
        return {status: 400,message: 'FAIL',err: e}
    }
} 


module.exports = {
    businessRegister: businessRegister,
    businessLogin: businessLogin
}